package com.easy.secret.utils;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @ProjectName: esay-secret-server
 * @Package: com.easy.secret.utils
 * @ClassName: RandomFileUtil
 * @Description: java类作用描述
 * @Author: wangjc
 * @CreateDate: 2023/11/17
 * @UpdateUser: wangjc
 * @UpdateDate: 2023/11/17
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class RandomFileUtil {

    /**
     * 随机文件加密
     *
     * @param f
     * @param key
     * @throws Exception
     */
    private static void encriptFile(File f, int key) throws Exception {
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
        int b;
        while ((b = raf.read()) != -1) {
            b ^= key;
            raf.seek(raf.getFilePointer() - 1);
            raf.write(b);
        }
        raf.close();
    }

    private static void getPrivateKeyFileData(byte[] data, int key) throws Exception {
        for(byte b: data) {
            b ^= key;
        }
        String s = new String(data);
    }

    /**
     * 读取秘钥文件
     *
     * @param filePath
     * @return
     * @throws IOException
     */
    public static byte[] readFile(String filePath) throws IOException {
        RandomAccessFile raf = null;
        byte[] data;
        try {
            raf = new RandomAccessFile(filePath, "r");
            data = new byte[(int)raf.length()];
            raf.read(data);
        } finally {
            if (raf != null) {
                raf.close();
            }
        }
        return data;
    }
}
